package com.shs.sys.common;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.IdUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Author: jxm
 * @Description: 文件上传、下载工具
 * @Date: 2020/4/16 18:31
 * @Version: 1.0
 */
public class SHFileUtils {

    //文件上传保存路径(默认)
    public static String UPLOAD_PATH="E:/upload/";

    static {
        //读取配置文件的保存路径
        InputStream stream = SHFileUtils.class.getClassLoader().getResourceAsStream("file.properties");
        Properties properties = new Properties();
        try {
            properties.load(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String property = properties.getProperty("filepath");
        if(null!=property){
            UPLOAD_PATH = property;
        }
    }

    /**
     * 替换旧的文件名
     * @param oldFileName
     * @return
     */
    public static String createNewFileName(String oldFileName){
        //获取到原文件名的后缀（包括'.'）
        String substring = oldFileName.substring(oldFileName.lastIndexOf("."), oldFileName.length());//左闭右开
        return IdUtil.simpleUUID().toUpperCase()+substring;
    }

    /**
     * 文件下载
     * @param path
     * @return
     */
    public static ResponseEntity<Object> createResponseEntity(String path){
        /**
         * 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
         * File(String parent, String child)
         */
        //1,构造文件对象
        File file = new File(UPLOAD_PATH,path);
        if(file.exists()){
            //将下载的文件，封装byte[]
            byte[] bytes = null;
            try {
                bytes = FileUtil.readBytes(file);
            } catch (IORuntimeException e) {
                e.printStackTrace();
            }
            //创建封装响应头信息的对象
            HttpHeaders header=new HttpHeaders();
            //封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
            header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //设置下载的文件的名称
//			header.setContentDispositionFormData("attachment", "123.jpg");
            //创建ResponseEntity对象
            ResponseEntity<Object> entity=
                    new ResponseEntity<Object>(bytes, header, HttpStatus.CREATED);
            return entity;
        }
        return null;
    }

    /**
     * 根据路径改名字 去掉_temp
     * @param goodsimg
     * @return
     */
    public static String reNameFile(String goodsimg){
        //1,构造文件对象
        File file = new File(UPLOAD_PATH,goodsimg);
        String replace = goodsimg.replace("_temp", "");
        if(file.exists()){
            file.renameTo((new File(UPLOAD_PATH,replace)));
        }
        return replace;
    }

    /**
     * 根据老路径删除图片
     * @param oldPath
     */
    public static void removeFileByPath(String oldPath){
        if(!oldPath.equals(Constants.IMAGES_DEFAULTGOODSIMG_PNG)){
            File file = new File(UPLOAD_PATH,oldPath);
            if(file.exists()){
                file.delete();
            }
        }
    }
}
